#This is to get:
#Figure 2

##############
# Libraries  #
##############
rm(list = ls())
library("visreg")

setwd("/Users/bgpopescu/Dropbox/Legacies_Central_Europe/")
setwd("C:/Users/bogdanp/Dropbox/Legacies_Central_Europe/")
#In this file we examine the effect of the distance to printing presses on literacy.
#Historical printing presses were in the following locations:
#Cluj, Tirgiviste, Alba-Iulia, Campulung, Sibiu, Govora, Brasov, Iasi.

#Step1: Reading CVS file
print_press_ro<-read.csv(file="./data/plasi_1930.csv")

#Step2: Selecting only observations within 100km
plasi_1930_cluj<-subset(print_press_ro, Cluj_distance<100)
plasi_1930_cluj$printing_press_dist<-plasi_1930_cluj$Cluj_distance
plasi_1930_bucharest<-subset(print_press_ro, Bucharest_distance<100)
plasi_1930_bucharest$printing_press_dist<-plasi_1930_bucharest$Bucharest_distance
plasi_1930_tirgoviste<-subset(print_press_ro, Tirgoviste_distance<100)
plasi_1930_tirgoviste$printing_press_dist<-plasi_1930_tirgoviste$Tirgoviste_distance
plasi_1930_albaiulia<-subset(print_press_ro, Alba_Iulia_distance<100)
plasi_1930_albaiulia$printing_press_dist<-plasi_1930_albaiulia$Alba_Iulia_distance
plasi_1930_campulung<-subset(print_press_ro, Campulung_distance<100)
plasi_1930_campulung$printing_press_dist<-plasi_1930_campulung$Campulung_distance
plasi_1930_sibiu<-subset(print_press_ro, Sibiu_distance<100)
plasi_1930_sibiu$printing_press_dist<-plasi_1930_sibiu$Sibiu_distance
plasi_1930_govora<-subset(print_press_ro, Govora_distance<100)
plasi_1930_govora$printing_press_dist<-plasi_1930_govora$Govora_distance
plasi_1930_brasov<-subset(print_press_ro, Brasov_distance<100)
plasi_1930_brasov$printing_press_dist<-plasi_1930_brasov$Brasov_distance
plasi_1930_iasi<-subset(print_press_ro, Iasi_distance<100)
plasi_1930_iasi$printing_press_dist<-plasi_1930_iasi$Iasi_distance

#Step3: Binding all places
#Note: We remove Bucharest to remove the effect of the capital
final<-rbind(plasi_1930_cluj, plasi_1930_tirgoviste,
             plasi_1930_albaiulia, plasi_1930_campulung, plasi_1930_sibiu, 
             plasi_1930_govora, plasi_1930_brasov, plasi_1930_iasi)

#Step4: Cleaning workspace
rm(plasi_1930_cluj, plasi_1930_tirgoviste,
   plasi_1930_albaiulia, plasi_1930_campulung, plasi_1930_sibiu, 
   plasi_1930_govora, plasi_1930_brasov, plasi_1930_iasi, plasi_1930_bucharest,
   print_press_ro)

press_trans<-lm(pct_literate ~log(printing_press_dist), data=final)
summary(press_trans) 

#Creating Figure
jpeg(filename="./Paper/graphs/figure2.jpg",   
     width     = 4,
     height    = 4,
     units     = "in",
     res       = 300,
     pointsize = 6)

visreg(press_trans, "printing_press_dist", scale="response",
       ylab="Pct. Literate, 1930", xlab="Distance to a town with Historical Printing Press, km", 
       #main="Literacy and Printing Pess", 
       fill.par=list(density = 15, angle = 90, col="blue", 
                     cex.axis = 3, cex.lab = 3), 
       line.par=list(col="black"))
dev.off()
